<?php
//decode by http://奥森世纪/
namespace app\home\model;

use think\Exception;
use think\Log;
class MyTaskAudit extends Base
{
    protected $table = 'tb_task_join';
    private static function _get_where($params = [], $is_count = false)
    {
        $where = "task_join.task_id=" . (isset($params['task_id']) ? intval($params['task_id']) : -1);
        $where .= " AND task_join.delflag=1 AND task.is_display=1 ";
        if (check_array($params)) {
            if (!empty($params['category_type'])) {
                switch ($params['category_type']) {
                    case "lock":
                        $where .= " AND task_join.status=1 ";
                        break;
                    case "wait":
                        $where .= " AND task_join.status=2 ";
                        break;
                    case "pass":
                        $where .= " AND task_join.status=3 ";
                        break;
                    case "nopass":
                        $where .= " AND task_join.status=4 ";
                        break;
                    default:
                }
            }
        }
        if (!$is_count) {
            $where .= " ORDER BY task_join.update_time DESC";
        }
        return $where;
    }
    public static function getListByParams($params = [], $psize = 15)
    {
        $page = get_now_page();
        $pindex = ($page - 1) * $psize;
        $where = self::_get_where($params);
        $sql = "SELECT task.*, task_join.id as join_id, task_join.uid as join_uid,task_join.times as join_times, task_join.status as join_status, task_join.create_time as join_create_time, task_join.audit_time as join_audit_time FROM tb_task_join task_join INNER JOIN tb_task task ON task_join.task_id=task.id WHERE {$where} LIMIT {$pindex},{$psize}";
        $tasks = self::query($sql);
        if (!empty($tasks)) {
            $no = $pindex + 1;
            foreach ($tasks as &$v) {
                $v['no'] = $no++;
                $v['percent'] = round(floatval($v['join_num'] / $v['ticket_num'] * 100), 2);
                $v['end_time'] = date('Y-m-d H:i', $v['end_time']);
                $v['join_create_time'] = date('Y-m-d H:i', $v['join_create_time']);
                $v['join_audit_time'] = date('Y-m-d H:i', $v['join_audit_time']);
            }
        }
        return $tasks;
    }
    public static function getCountByParams($params = [], $psize = 15)
    {
        try {
            $where = self::_get_where($params, true);
            $sql = "SELECT COUNT(*) AS num FROM tb_task_join task_join INNER JOIN tb_task task ON task_join.task_id=task.id WHERE {$where}";
            $info = self::query($sql);
            return !empty($info[0]['num']) ? $info[0]['num'] : 0;
        } catch (Exception $e) {
            Log::error(__FILE__ . ':' . __LINE__ . ' 错误：' . $e->getMessage());
            return null;
        }
    }
}